<?php
declare (strict_types = 1);

namespace app\home\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class pyg_cart extends Model
{
    public function add($data)
    {
        $key = $this->where('user_id',$data['user_id'])->find();
        $rey = $this->where('spec_goods_id',$data['spec_goods_id'])->where('user_id',$data['user_id'])->find();
        if($key)
        {
            if($rey)
            {
                 $data['number'] = $rey->number+$data['number'];
                 return $this->where('spec_goods_id',$data['spec_goods_id'])->update($data);
            }
            return  $this->save($data);
        }
        return  $this->save($data);
    }
    public function showCart()
    {
        return $this->belongsTo(pyg_goods::class,'goods_id','id')->bind(['goods_name']);
    }
    public function showCarts()
    {
        return $this->belongsTo(pyg_spec_goods::class,'spec_goods_id','id')->bind(['value_names','price']);
    }
    public function reduction($id)
    {
        $data = $this->where('id',$id)->find();
        if($data)
        {
            if($data['number']>1)
            {
                $data['number']-=1;
                $data->save();
                return true;
            }
            return false;
        }
        return false;
    }
    public function sumCount($id)
    {
        $data = $this->where('id',$id)->find();
        if($data)
        {
                $data['number']+=1;
                $data->save();
                return true;
        }
        return false;
    }
}
